angular.module('app.controllers', [])

.controller('loginCtrl', function($scope,$state,$location,$ionicPopup,$http) {
    $scope.login = function (askCode, stunum, stuname) {
        var url = "http://115.159.225.131/vaildInfo";
        $http.post(url, {
            askCode: askCode,
            stunum: stunum,
            stuname: stuname
        }).success(function (data, header, config, status) {
            console.log(data);
            if (data.state == 1) {
                $state.go('page',{stunum: stunum,askcode: askCode,mode:data.mode,index:data.index});
            } else if (data.state == 4) {
                $state.go('page',{stunum: stunum,askcode: askCode,mode:data.mode,index:data.index});
                $ionicPopup.alert({
                    title: '登陆成功',
                    template: data.error
                });
            } else {
                $ionicPopup.alert({
                    title: '出错啦',
                    template: data.error
                });
            }
        }).error(function (data, header, config, status) {
            $ionicPopup.alert({
                title: '出错啦',
                template: '鞭打网络中...'
            });
        });

    }
})

.controller('signupCtrl', function($scope) {

})

.controller('pageCtrl', function($scope,$http,$ionicPopup,$location,$stateParams) {
    $scope.model={choice:''};
    $scope.more = {answer:''};
    idx = 1;
    $scope.index = {num:idx};
    $scope.expression2 = true;
    $scope.expression3 = true;
    $scope.mMode = "";
    $scope.mCode= "";
    $scope.showAsk = "显示题目";
    $scope.refershAsk = "刷新题目";
    $scope.askIsShow = true;
    askMode = 1;
    now_index = 1;
    stunum = null;

    $scope.mCode= $stateParams.askcode;
    askMode = $stateParams.mode;
    stunum = $stateParams.stunum;
    askCode = $stateParams.askcode;

    if(askMode == null)
    {
        $location.path("/login");
        $ionicPopup.alert({
            title: '出错啦',
            template: '请先登录'
        });
    }

    if(askMode == 1) {
        $scope.nowIndex = $stateParams.index;
    }else {
        $scope.nowIndex = 1;
    }
    changeModeState();

    $scope.showBtn = function (nowIndex) {
        if($scope.askIsShow == true)
        {
            $scope.askIsShow = false;
            $scope.showAsk = "隐藏题目";
            getnow($scope.nowIndex);
        }else{
            $scope.askIsShow = true;
            $scope.showAsk = "显示题目";
        }
    }

    $scope.refAsk = function () {
        getnow($scope.nowIndex);
    }
    
    $scope.choiceChange = function (choice) {
        temp = choice;
        if(temp == "Other")
        {
            $scope.expression2 = false;
        }else{
            $scope.more.answer = "";
            $scope.expression2 = true;
        }
    }
	


      $scope.setCode = function () {
        answer = $scope.model.choice;
        moreanswer = $scope.more.answer;
        if(answer == "Other")
        {
            answer = moreanswer;
        }
        selectIdx = $scope.nowIndex;
        console.log($scope.model);
        var map = {
          askCode: askCode,
          stunum: stunum,
          index:selectIdx,
          answer: answer,
          otheranswer: moreanswer,
        };
        //console.log(map);
        var url = "http://115.159.225.131/answer";
          if(answer == "" || answer == null)
          {
              $ionicPopup.alert({
                  title: '出错啦!',
                  template: "请选择答案!"
              });
          }else {
              $http.post('http://115.159.225.131/answer', {
                  askCode: askCode,
                  stunum: stunum,
                  index: selectIdx,
                  answer: answer,
                  moreAnswer: moreanswer,
              }).success(function (data, header, config, status) {
                  console.log(data);
                  $scope.expression = true;
                  if (data.state == 1) {
                      idx = data.correct.index;
                      idx++;
                      console.log(idx);
                      askMode = data.mode;
                      changeModeState();
                      //清空答案,设置其他答案框不可用
                      $scope.model.choice = null;
                      $scope.more.answer = '';
                      $scope.expression2 =true;
                      $scope.nowIndex = idx;

                      // if (askMode == 2) {
                      //     $scope.askIsShow = false;
                      //     $scope.showAsk = "隐藏题目";
                      getnow(idx);
                      // } else {
                      //     $scope.askIsShow = true;
                      //     $scope.showAsk = "显示题目";
                      // }
                      $scope.problemP = idx + ". ";
                      $scope.ChoiceA = null;
                      $scope.ChoiceD = null;
                      $scope.ChoiceC = null;
                      $scope.ChoiceB = null;

                      answerResult = "你的答案为" + data.correct.answer;
                      $ionicPopup.alert({
                          title: '提交成功',
                          template: answerResult
                      });
                  } else if (data.state == 2) {

                      //if (askMode == 2) {
                      idx = $scope.nowIndex;
                      idx++;
                      $scope.nowIndex = idx;
                      getnow(idx);
                      //}
                      askMode = data.mode;
                      changeModeState();
                      //清空答案,设置其他答案框不可用
                      $scope.model.choice = null;
                      $scope.more.answer = '';
                      $scope.expression2 =true;
                      $ionicPopup.alert({
                          title: '出错啦',
                          template: data.error
                      });
                  }
                  else if (data.state == 5) {
                      idx = data.nowIndex;
                      askMode = data.mode;
                      changeModeState();
                      // if (askMode == 2) {
                      //     $scope.askIsShow = false;
                      //     $scope.showAsk = "隐藏题目";
                      //     getnow(idx);
                      // } else {
                      //     $scope.askIsShow = true;
                      //     $scope.showAsk = "显示题目";
                      // }
                      $scope.nowIndex = idx;
                      indexText = "当前题号为：" + idx;
                      //清空答案,设置其他答案框不可用
                      $scope.model.choice = null;
                      $scope.more.answer = '';
                      $scope.expression2 =true;
                      getnow(idx)
                      $ionicPopup.alert({
                          title: '',
                          template: indexText
                      });
                  }
                  else {
                      askMode = data.mode;
                      changeModeState();
                      $ionicPopup.alert({
                          title: '出错啦',
                          template: data.error
                      });
                  }
              }).error(function (data, header, config, status) {
                  $ionicPopup.alert({
                      title: '出错啦',
                      template: '鞭打网络中...'
                  });
              });
          }
      }


    $scope.changeIndex = function (jumpToIndex) {
        //jumpToIndex = $scope.jumpIndex;

        console.log(jumpToIndex);
        console.log($scope.jumpIndex);
        getnow(jumpToIndex);
        $scope.nowIndex = jumpToIndex;
    }

    function getnow(index)
    {
        $http.post('http://115.159.225.131/getnow', {
            askCode: $scope.mCode,
            index:index
        }).success(function (data,header,config,status) {
            if(data.state == 0) {
                $scope.nowIndex = data.maxNum;
                $ionicPopup.alert({
                    title: '出错啦',
                    template: data.error

                });
            }else {
                $scope.problemP = data.question.index + ". " + data.question.timu;
                if (data.question.A == null) {
                    console.log(data.question.A);
                    console.log(data.question.B);
                    console.log(data.question.C);
                    console.log(data.question.D);
                }

                if (data.question.A != '' && data.question.A != null) {
                    $scope.ChoiceA = "A. " + data.question.A;
                } else {
                    $scope.ChoiceA = ' ';
                }
                if (data.question.B != '' && data.question.B != null) {
                    $scope.ChoiceB = "B. " + data.question.B;
                } else {
                    $scope.ChoiceB = ' ';
                }
                if (data.question.C != '' && data.question.C != null) {
                    $scope.ChoiceC = "C. " + data.question.C;
                } else {
                    $scope.ChoiceC = ' ';
                }
                if (data.question.D != '' && data.question.D != null) {
                    $scope.ChoiceD = "D. " + data.question.D;
                } else {
                    $scope.ChoiceD = ' ';
                }
            }
        }).error(function(data,header,config,status){
            $ionicPopup.alert({
                title: '出错啦',
                template: '鞭打网络中...'
            });
        });
    }


    function changeModeState() {
        if(askMode == 1) {
            $scope.mMode = "教师控制模式";
            $scope.canJumpToIndex = "无法更改题号";
            $scope.jumpIndex = "";
            $scope.canJump = true;
        }else {
            $scope.mMode = "自由答题模式";
            $scope.canJumpToIndex = "可以更改题号";
            $scope.canJump = false;
            $scope.jumpIndex = "";
        }
    }


})






